package cn.myfirefly.moming.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import cn.myfirefly.moming.common.entity.MMUser;
import cn.myfirefly.moming.common.model.UserDto;
import cn.myfirefly.moming.common.model.UserFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.FilterUtil;
import cn.myfirefly.utils.StrUtil;

@Repository
public class UserDao extends BaseDao<MMUser>{
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public PageResultDto<MMUser> getList(UserFilterDto dto, boolean isAdmin) {
		Session session = sessionFactory.getCurrentSession();
		PageResultDto<MMUser> resultDto = new PageResultDto<>();
		
		StringBuffer countSql = new StringBuffer("select count(u) from MMUser u where 1=1 ");
		StringBuffer querySql = new StringBuffer("from MMUser u where 1=1 ");
		if (isAdmin == false) {
			countSql.append(" and u.userSn!=1");
			querySql.append(" and u.userSn!=1");
		}
		List paramList = new ArrayList();
		FilterUtil.handleFilterSql(dto, "u", countSql, querySql, paramList);
		
		Query query = session.createQuery(countSql.toString());
		FilterUtil.setQueryParams(query, paramList);
		Number number = (Number)query.uniqueResult();
		resultDto.setTotal(number.intValue());
		if (number.intValue() > 0) {
			query = session.createQuery(querySql.toString());
			FilterUtil.setQueryParams(query, paramList);
			List<MMUser> list = query
					.setFirstResult((dto.getPageCurrent() - 1) * dto.getPageSize())
					.setMaxResults(dto.getPageSize())
					.list();
			resultDto.setPageCurrent(dto.getPageCurrent());
			resultDto.setList(list);
		}
		return resultDto;
	}
	
	@SuppressWarnings("unchecked")
	public PageResultDto<MMUser> getList(UserDto dto) {
		Session session = sessionFactory.getCurrentSession();
		PageResultDto<MMUser> resultDto = new PageResultDto<>();
		
		StringBuffer countSql = new StringBuffer("select count(u) from MMUser u where 1=1 ");
		StringBuffer querySql = new StringBuffer("from MMUser u where 1=1 ");
		if (StrUtil.notBlank(dto.getLoginName())) {
			countSql.append(" and u.loginName like :loginName ");
			querySql.append(" and u.loginName like :loginName ");
		}
		Query query = session.createQuery(countSql.toString());
		if (StrUtil.notBlank(dto.getLoginName())) {
			query.setString("loginName", "%" + dto.getLoginName() + "%");
		}
		Number number = (Number)query.uniqueResult();
		resultDto.setTotal(number.intValue());
		if (number.intValue() > 0) {
			query = session.createQuery(querySql.toString());
			if (StrUtil.notBlank(dto.getLoginName())) {
				query.setString("loginName", "%" + dto.getLoginName() + "%");
			}
			List<MMUser> list = query
					.setFirstResult((dto.getPageCurrent() - 1) * dto.getPageSize())
					.setMaxResults(dto.getPageSize())
					.list();
			resultDto.setPageCurrent(dto.getPageCurrent());
			resultDto.setList(list);
		}
		return resultDto;
	}
	
	@SuppressWarnings("unchecked")
	public MMUser getUserByNo(String userNo) {
		Session session = sessionFactory.getCurrentSession();
		MMUser user = null;
		Query query = session.createQuery("from MMUser u where u.userNo=? ");
		query.setString(0, userNo);
		List<MMUser> list = query.list();
		if (list != null && list.size() > 0) {
			user = list.get(0);
		}
		return user;
	}
	
	@SuppressWarnings("unchecked")
	public MMUser getUserByLoginName(String loginName) {
		Session session = sessionFactory.getCurrentSession();
		MMUser user = null;
		Query query = session.createQuery("from MMUser u where u.loginName=? ");
		query.setString(0, loginName);
		List<MMUser> list = query.list();
		if (list != null && list.size() > 0) {
			user = list.get(0);
		}
		return user;
	}
	
}
